package experiment3choose;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 定义 Employee 类
class Employee {
    private String department;
    private double salary;

    public Employee(String department, double salary) {
        this.department = department;
        this.salary = salary;
    }

    public String getDepartment() {
        return department;
    }

    public double getSalary() {
        return salary;
    }
}

public class MapGroupingStatistics {
    public static void main(String[] args) {
        // 创建员工列表
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee("技术部", 5000));
        employees.add(new Employee("市场部", 6000));
        employees.add(new Employee("技术部", 4500));
        employees.add(new Employee("市场部", 5500));
        employees.add(new Employee("财务部", 4000));

        // 使用 HashMap 对员工按部门分组，并统计每个部门的总工资
        Map<String, Double> departmentTotalSalary = new HashMap<>();
        for (Employee employee : employees) {
            String department = employee.getDepartment();
            double salary = employee.getSalary();
            departmentTotalSalary.put(department, departmentTotalSalary.getOrDefault(department, 0.0) + salary);
        }

        // 打印每个部门及其总工资
        System.out.println("各部门总工资统计：");
        for (Map.Entry<String, Double> entry : departmentTotalSalary.entrySet()) {
            System.out.println(entry.getKey() + "：" + entry.getValue());
        }
    }
}